﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using Sitecore.Data.Items;
using Workflow.Alerts.DAL;
using workflow.Dal;

namespace Workflow.Alerts
{
    public class FilterViewPresenter : Presenter
    {
        #region members
        private readonly IFilterView view;
        #endregion

        #region constructor
        public FilterViewPresenter(IFilterView _view)
            : this(_view, null)
        { }

        public FilterViewPresenter(IFilterView _view, ISessionProvider session)
            : base(_view, session)
        {
            view = base.GetView<IFilterView>();
            view.FilterLoad += view_OnPageLoad;
            view.FilterApply += view_OnPageSave;
        }
        #endregion

        #region private methods

        private void view_OnPageLoad()
        {

            view.AlertTypeList = WorkflowEnums.AlertTypeList;
            view.ActivityTypeList = WorkflowEnums.ActivityTypeList;

            if (CurrentUser.Type == WorkflowEnums.UserType.Municipality)
            {
                //get networkoperator and contractors
                NetworkOperatorDAL dal = new NetworkOperatorDAL();
                WorkflowFilter filter = new WorkflowFilter { MunicipalityId = CurrentUser.TypeId };
                WorkflowFilterSql filterSql = new WorkflowFilterSql(filter);
                view.NetworkOperatorList = dal.GetNetworkOperatorList(filterSql);
            }
            else if (CurrentUser.Type == WorkflowEnums.UserType.NetworkOperator)
            {
                //get municipality
                NewAlertDAL dal = new NewAlertDAL();
                WorkflowFilter filter = new WorkflowFilter
                {
                    NetworkOperatorId = CurrentUser.TypeId
                };
                WorkflowFilterSql filterSql = new WorkflowFilterSql(filter);
                view.MunicipalityList = dal.GetMunicipalityByNetworkOperator(filterSql);

            }
            view.StatusList = GetStatusList(Utils.Utils.GetCurrentItem());

            //GET LIST OF POSSIBLE DATES FROM SITECORE
            Dictionary<string, string> filterDates = new Dictionary<string, string>();
            filterDates.Add("actualstartdate", "Definitieve start datum");
            filterDates.Add("actualenddate", "Definitieve eind datum");
            filterDates.Add("estimatestartdate", "Voorgenomen startdatum");
            filterDates.Add("estimateenddate", "Voorgenomen einddatum");
            filterDates.Add("creationdate", "Aanmaakdatum");
            view.Dates = filterDates;

            using (WorkflowEntitySet context = new WorkflowEntitySet())
            {
                var contractors = context.Contractor.ToList<Contractor>();
                Dictionary<int, string> Contractors = new Dictionary<int, string>();
                foreach (var c in contractors)
                {
                    Contractors.Add(c.id, c.name);
                }

                view.ContractorList = Contractors;
            }

            //get filter values from session
            if (Filters != null)
            {
                if (Filters.SelectedActivitiyType != null)
                    view.SelectedActivityType = Filters.SelectedActivitiyType;
                if (Filters.SelectedAlertType != null)
                    view.SelectedAlertType = Filters.SelectedAlertType;
                if (Filters.SelectedNetworkOperators != null)
                    view.SelectedNetworkOperator = Filters.SelectedNetworkOperators;
                if (Filters.SelectedMunicipalities != null)
                    view.SelectedMunicipality = Filters.SelectedMunicipalities;
                if (Filters.SelectedStatuses != null)
                    view.SelectedSatuses = Filters.SelectedStatuses;
                if (Filters.Dates != null)
                    Session[WorkflowConstants.FilterDates] = Filters.Dates;
                view.Contractor = Filters.SelectedContractor;
                view.IsHerstel = Filters.IsHerstel;
            }
        }

        private Dictionary<int, string> GetStatusList(string item)
        {
            var result = new Dictionary<int, string>();

            if (CurrentUser.Type == WorkflowEnums.UserType.Municipality)
            {
                switch (item)
                {
                    case WorkflowConstants.NewAlerts:
                        result.Add((int)WorkflowEnums.Status.Sent, "Verzonden");
                        result.Add((int)WorkflowEnums.Status.Approved, "Akkoord");
                        result.Add((int)WorkflowEnums.Status.PartiallyApproved, "Akkoord onder voorbehoud");
                        result.Add((int)WorkflowEnums.Status.Rejected, "Niet akkoord");
                        break;
                    case WorkflowConstants.InProgress:
                        result.Add((int)WorkflowEnums.Status.Approved, "Akkoord");
                        result.Add((int)WorkflowEnums.Status.PartiallyApproved, "Akkoord onder voorbehoud");
                        result.Add((int)WorkflowEnums.Status.Executed4Contractor, " In uitvoering");
                        result.Add((int)WorkflowEnums.Status.Declined, "Niet akkoord");
                        break;
                    case WorkflowConstants.Executed:
                        result.Add((int)WorkflowEnums.Status.Executed4Municipality, "Lopend");
                        result.Add((int)WorkflowEnums.Status.Accepted, "Akkoord");
                        result.Add((int)WorkflowEnums.Status.Declined, "Niet akkoord");                        

                        break;
                    case WorkflowConstants.Logged:
                        result.Add((int)WorkflowEnums.Status.Accepted, "Akkoord");
                        result.Add((int)WorkflowEnums.Status.AcceptedPartially, "Akkoord onder voorbehoud");
                        result.Add((int)WorkflowEnums.Status.Rejected, "Niet akkoord");
                        break;
                }
            }
            else if (CurrentUser.Type == WorkflowEnums.UserType.NetworkOperator)
            {
                switch (item)
                {
                    case WorkflowConstants.InProgress:
                        result.Add((int)WorkflowEnums.Status.Approved, "Akkoord");
                        result.Add((int)WorkflowEnums.Status.PartiallyApproved, "Akkoord onder voorbehoud");
                        result.Add((int)WorkflowEnums.Status.Declined, "Niet akkoord");
                        break;
                    case WorkflowConstants.Logged:
                        result.Add((int)WorkflowEnums.Status.Accepted, "Akkoord");
                        result.Add((int)WorkflowEnums.Status.AcceptedPartially, "Akkoord onder voorbehoud");
                        result.Add((int)WorkflowEnums.Status.Rejected, "Niet akkoord");
                        break;
                }
            }
            else
            {
                //Contractor
            }

            return result;
        }

        //store filter settings in session 
        private void view_OnPageSave()
        {

            Filters.SelectedActivitiyType = view.SelectedActivityType;
            Filters.SelectedAlertType = view.SelectedAlertType;
            Filters.SelectedMunicipalities = view.SelectedMunicipality;
            Filters.SelectedStatuses = view.SelectedSatuses;
            Filters.SelectedNetworkOperators = view.SelectedNetworkOperator;
            Filters.Dates = Session[WorkflowConstants.FilterDates] as Dictionary<string, DateTime>;
            Filters.SelectedContractor = view.Contractor;
            Filters.IsHerstel = view.IsHerstel;
        }
        #endregion
    }

    public class Filter
    {
        public List<int> SelectedAlertType { get; set; }
        public List<int> SelectedActivitiyType { get; set; }
        public List<int> SelectedStatuses { get; set; }
        public List<int> SelectedNetworkOperators { get; set; }
        public List<int> SelectedMunicipalities { get; set; }

        public Dictionary<string, DateTime> Dates { get; set; }
        public Int32? SelectedContractor { get; set; }
        public Boolean IsHerstel { get; set; }
    }
}